package com.haojii.jdo.orm.many2many;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.Order;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;


@PersistenceCapable(identityType = IdentityType.APPLICATION) 
public class EasytvEntity {
	
	@Persistent
	String title;

	@PrimaryKey
	String url;
	//Map<String,String> linksMap = new HashMap<String, String>();
	//Map<String,String> newLinksMap = new HashMap<String, String>();
	
/*	@Persistent(defaultFetchGroup="true",mappedBy = "easytvEntity") 
	private List<EasytvItem> items = new ArrayList<EasytvItem>();*/
	@Persistent(defaultFetchGroup="true")
    @Order(extensions = @Extension(vendorName="datanucleus", key="list-ordering", value="itemLink asc"))
    private List<EasytvItem> items;

	
	@NotPersistent 
	private List<EasytvItem> newItems = new ArrayList<EasytvItem>();
	
	@Persistent
	private Set<Key> watchingUsers = new HashSet<Key>();

	public EasytvEntity(String title, String url) {
		this.title = title;
		this.url = url;
	}
	
	public String getTitle() {
		return title;
	}

	public String getUrl() {
		return url;
	}


	@Override
	public String toString() {
		return "Item [linksMap=" + items + ", newLinksMap=" + newItems
				+ ", title=" + title + ", url=" + url + "]";
	}

	public String message() {
		StringBuilder sb = new StringBuilder();
		
		sb.append("New!!!");
		sb.append("\n");
		sb.append(title);
		sb.append("\n");
		for (EasytvItem e : newItems)
		{
			sb.append(e.itemTitle);
			sb.append("\n");
		}
		
		return sb.toString();
	}

	public String emailBodyText() {
		StringBuilder sb = new StringBuilder();
		
		sb.append("New!!!");
		sb.append("<br/>\n");
		sb.append(title);
		sb.append("<br/>\n");
		sb.append("<a href=\""+url+"\">"+title+"</a>");
		sb.append("<br/>\n");
		for (EasytvItem e : newItems)
		{
			sb.append("<a href=\""+e.itemLink+"\">"+e.itemTitle+"</a>");
			sb.append("<br/>\n");
		}
		
		return sb.toString();
	}

	public String plainMessage() {
		return message();
	}

	public String smsMessage() {
		return message();
	}

	public String emailSubject() {
		return "New!!!"+title;
	}
	
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((url == null) ? 0 : url.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		EasytvEntity other = (EasytvEntity) obj;
		if (url == null) {
			if (other.url != null)
				return false;
		} else if (!url.equals(other.url))
			return false;
		return true;
	}
	
	public List<EasytvItem> getItems() {
		if (items == null)
		{
			items = new ArrayList<EasytvItem>();
		}
		return items;
	}

	public void setItems(List<EasytvItem> items) {
		this.items = items;
	}

	public List<EasytvItem> getNewItems() {
		if (newItems == null)
		{
			newItems = new ArrayList<EasytvItem>();
		}
		return newItems;
	}

	public void setNewItems(List<EasytvItem> newItems) {
		this.newItems = newItems;
	}

	public Set<Key> getWatchingUsers() {
		return watchingUsers;
	}

	public void setWatchingUsers(Set<Key> watchingUsers) {
		this.watchingUsers = watchingUsers;
	}
	public Key getKey()
	{
		return KeyFactory.createKey(EasytvEntity.class.getSimpleName(), url);
	}

}
